<template>
  <div class="add">
    <el-card class="add-container">
      <el-form :model="state.admin" :rules="state.rules" ref="goodRef" label-width="100px" class="goodForm">
        <el-form-item label="管理员名称" prop="Name">
          <el-input style="width: 300px" v-model="state.admin.loginUserName" placeholder="请输入管理员名称"></el-input>
        </el-form-item>
        <el-form-item label="管理员密码" prop="pws">
          <el-input style="width: 300px" v-model="state.admin.loginPassword" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="管理员昵称" prop="Name">
          <el-input style="width: 300px" v-model="state.admin.nickName" placeholder="请输入管理员名称"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="Name">
          <el-input style="width: 300px" v-model="state.admin.locked" placeholder="请输入0或1"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitAdd()">立即创建</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import {reactive, ref, onMounted, onBeforeUnmount, getCurrentInstance} from 'vue'
import WangEditor from 'wangeditor'
import axios from '@/utils/axios'
import {ElMessage} from 'element-plus'
import {useRoute, useRouter} from 'vue-router'
import {localGet, uploadImgServer, uploadImgsServer} from '@/utils'

const {proxy} = getCurrentInstance()
const editor = ref(null)
const goodRef = ref(null)
const route = useRoute()
const router = useRouter()
const {id} = route.query
const state = reactive({
  admin: {
    loginUserName: '',
    loginPassword: '',
    nickName: '',
    locked: ''
  },
  rules: {
    userName: [
      {required: 'true', message: '请填写名称', trigger: ['change']}
    ],
    password: [
      {required: 'true', message: '请填写密码', trigger: ['change']}
    ],
    nickName: [
      {required: 'true', message: '请填写昵称', trigger: ['change']}
    ],
    locked: [
      {required: 'true', message: '请填写是否启用', trigger: ['change']}
    ],
  },
})

// 添加管理员
const submitAdd = async () => {
  try {
    await goodRef.value.validate()
    const {data: res} = await axios.post('/adminUser/addNew', state.admin)
    if (res.status === 200) {
      ElMessage.success(res.msg)
      router.push('/admin')
    } else {
      ElMessage.error(res.msg)
    }
  } catch (err) {
    console.log(err)
  }
}

</script>

<style scoped>
.add {
  display: flex;
}

.add-container {
  flex: 1;
  height: 100%;
}

.avatar-uploader {
  width: 100px;
  height: 100px;
  color: #ddd;
  font-size: 30px;
}

.avatar-uploader-icon {
  display: block;
  width: 100%;
  height: 100%;
  border: 1px solid #e9e9e9;
  padding: 32px 17px;
}
</style>